package com.easylinkin.linkappapi.webcammanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.openapi.dto.VideoLimitDTO;
import com.easylinkin.linkappapi.openapi.service.OpenApiService;
import com.easylinkin.linkappapi.webcammanage.entity.ImageCapture;
import com.easylinkin.linkappapi.webcammanage.entity.QueryImageCapture;
import com.easylinkin.linkappapi.webcammanage.service.ImageCaptureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * class info :汶川草原--图像抓拍功能
 *
 * @author liuqihang
 * @date 2021/6/18 10:35
 */
@RestController
@RequestMapping("/imageCapture")
@Slf4j
public class ImageCaptureController {
    
    @Resource
    private ImageCaptureService imageCaptureService;

    @Resource
    private OpenApiService openApiService;

    @PostMapping("getPage")
    public RestMessage getPage(@RequestBody RequestModel<QueryImageCapture> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<ImageCapture> page = imageCaptureService.getPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(page).build();
    }

    @PostMapping("batchDownloadZip")
    public String batchDownloadZip(@RequestBody QueryImageCapture queryImageCapture, HttpServletRequest request, HttpServletResponse response) {
        String result = imageCaptureService.batchDownloadZip(queryImageCapture, request, response);
        if(result != null){
            return result;
        }else {
            return "success";
        }
    }

    @GetMapping("getVideoFlow/{deviceCode}")
    public RestMessage getVideoFlow(@PathVariable("deviceCode") String deviceCode) {
        VideoLimitDTO videoLimitDTOByDeviceCode = null;
        try {
            videoLimitDTOByDeviceCode = openApiService.getVideoLimitDTOByDeviceCode(deviceCode);
            if(videoLimitDTOByDeviceCode == null){
                return RestBuilders.failureMessage().setMessage("调用linkthings接口响应失败");
            }
        } catch (UnsupportedEncodingException e) {
            return RestBuilders.failureMessage().setMessage("服务器异常");
        }
        return RestBuilders.successBuilder().data(videoLimitDTOByDeviceCode).build();
    }

}
